package cn.iocoder.yudao.module.handguard.controller.admin.publicitytype;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.admin.publicitytype.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.publicitytype.PublicityTypeDO;
import cn.iocoder.yudao.module.handguard.convert.publicitytype.PublicityTypeConvert;
import cn.iocoder.yudao.module.handguard.service.publicitytype.PublicityTypeService;

@Tag(name = "管理后台 - 16个公示")
@RestController
@RequestMapping("/hutai/publicity-type")
@Validated
public class PublicityTypeController {

    @Resource
    private PublicityTypeService publicityTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建16个公示")
    @PreAuthorize("@ss.hasPermission('hutai:publicity-type:create')")
    public CommonResult<Long> createPublicityType(@Valid @RequestBody PublicityTypeCreateReqVO createReqVO) {
        return success(publicityTypeService.createPublicityType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新16个公示")
    @PreAuthorize("@ss.hasPermission('hutai:publicity-type:update')")
    public CommonResult<Boolean> updatePublicityType(@Valid @RequestBody PublicityTypeUpdateReqVO updateReqVO) {
        publicityTypeService.updatePublicityType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除16个公示")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:publicity-type:delete')")
    public CommonResult<Boolean> deletePublicityType(@RequestParam("id") Long id) {
        publicityTypeService.deletePublicityType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得16个公示")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:publicity-type:query')")
    public CommonResult<PublicityTypeRespVO> getPublicityType(@RequestParam("id") Long id) {
        PublicityTypeDO publicityType = publicityTypeService.getPublicityType(id);
        return success(PublicityTypeConvert.INSTANCE.convert(publicityType));
    }

    @GetMapping("/list")
    @Operation(summary = "获得16个公示列表")
    public CommonResult<List<PublicityTypeRespVO>> getPublicityTypeList() {
        List<PublicityTypeDO> list = publicityTypeService.getPublicityTypeList();
        return success(PublicityTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/getPublicitySelectListByTypesName")
    @Operation(summary = "准入-获得公示表类目")
    public CommonResult<List<PublicitySelectBaseVO>> getPublicitySelectListByTypesName(Long id,Long orderId) {
        PublicityTypeDO publicityTypeDO = publicityTypeService.getPublicityType(id);
        String typesName = publicityTypeDO.getAliasName();
        List<PublicitySelectBaseVO> list = publicityTypeService.getPublicitySelectListByTypesName(typesName,orderId);
        List<PublicitySelectBaseVO> lists = new ArrayList<>();
        for (PublicitySelectBaseVO selectBaseVO : list) {
               // String element = publicitySelectBaseVO.getClassName();
               //姓名、性别输入项、合计项不需要填写
                if (selectBaseVO.getCategory().equals("0")  || selectBaseVO.getCategory().equals("1")
                        || selectBaseVO.getAddition().equals("0") || selectBaseVO.getCategory().equals("-1")
                        || selectBaseVO.getCategory().equals("2") || selectBaseVO.getCategory().equals("-2")) {
                }else {
                    lists.add(selectBaseVO);
                }
        }
        return success(lists);
    }

    @GetMapping("/getAgentPublicitySelectListByTypesName")
    @Operation(summary = "业务员-获得公示表类目")
    public CommonResult<List<PublicitySelectBaseVO>> getAgentPublicitySelectListByTypesName(Long id,Long orderId) {
        PublicityTypeDO publicityTypeDO = publicityTypeService.getPublicityType(id);
        String typesName = publicityTypeDO.getAliasName();
        List<PublicitySelectBaseVO> list = publicityTypeService.getPublicitySelectListByTypesName(typesName,orderId);
        return success(list);
    }

    @GetMapping("/getPublicitySelectListByName")
    @Operation(summary = "获得公示表类目2")
    public CommonResult<List<PublicitySelectBaseVO>> getPublicitySelectListByTypesName(String typesName,Long orderId) {
        List<PublicitySelectBaseVO> list = publicityTypeService.getPublicitySelectListByTypesName(typesName,orderId);
        return success(list);
    }

    @GetMapping("/getPublicitySelectListByClassName")
    @Operation(summary = "获得公示表类目值选项")
    public CommonResult<List<PublicitySelectBaseVO>> getPublicitySelectListByClassName(String className,String typesName) {
        List<PublicitySelectBaseVO> list = publicityTypeService.getPublicitySelectListByClassName(className,typesName);
        return success(list);
    }

    @GetMapping("/page")
    @Operation(summary = "获得16个公示分页")
    @PreAuthorize("@ss.hasPermission('hutai:publicity-type:query')")
    public CommonResult<PageResult<PublicityTypeRespVO>> getPublicityTypePage(@Valid PublicityTypePageReqVO pageVO) {
        PageResult<PublicityTypeDO> pageResult = publicityTypeService.getPublicityTypePage(pageVO);
        return success(PublicityTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出16个公示 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:publicity-type:export')")
    @OperateLog(type = EXPORT)
    public void exportPublicityTypeExcel(@Valid PublicityTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<PublicityTypeDO> list = publicityTypeService.getPublicityTypeList(exportReqVO);
        // 导出 Excel
        List<PublicityTypeExcelVO> datas = PublicityTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "16个公示.xls", "数据", PublicityTypeExcelVO.class, datas);
    }

}
